package com.panda.group.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.panda.group.entity.Group;
import com.panda.group.entity.GroupMember;
import com.panda.group.entity.GroupMemberAcce;
import com.panda.group.entity.MemberInfo;
import com.panda.group.service.GroupService;
import com.panda.progress.entity.Progress;
import com.panda.progress.service.ProgressService;
import com.panda.sys.entity.User;
import com.panda.topic.entity.Topic;
import com.panda.topic.service.TopicService;
import com.panda.utils.CommonUtils;
import com.panda.utils.Pager;
import com.panda.utils.contantUtil;

@Controller
public class GroupController {

	@Resource
	private GroupService groupService;
	@Resource
	private TopicService topicService;
	@Resource
	private ProgressService progressService;
	
	@RequestMapping("/group/list")
	public String list() {
		return "group/list";
	}
 
	@RequestMapping("/group/getlist")
	@ResponseBody
	public String getlist(Model model,Long offSet,Long pageSize,String name,HttpServletRequest request){
		User user = (User) request.getSession().getAttribute(contantUtil.SESSION_USER);
		Pager<Group> Groups=groupService.findListLimit(offSet, pageSize,user.getId(),name);
		return JSON.toJSONString(Groups);
	}
	
	@RequestMapping("/group/findAllTopic")
	@ResponseBody
	public String findAllTopic(){
		List<Topic> topics=topicService.findAllTopic();
		return CommonUtils.msgCallback(true,"查询成功",null, JSON.toJSONString(topics));
	}
	
	@RequestMapping("/group/findAllStudent")
	@ResponseBody
	public String findAllStudent(Long id){
		List<User> users=groupService.findAllStudent();
		return CommonUtils.msgCallback(true,"查询成功",null, JSON.toJSONString(users));
	}
	
	@RequestMapping("/group/save")
	@ResponseBody
	public String save(String group,String member, HttpServletRequest request){
		List<GroupMember> members = JSON.parseArray(member, GroupMember.class);
		Group groups = JSON.parseObject(group, Group.class);
		User user = (User) request.getSession().getAttribute(contantUtil.SESSION_USER);
		groups.setBelong(user.getId());
		groupService.saveOrUpdate(groups,members);
		return CommonUtils.msgCallback(true,"保存成功",null,null);
	}
	
	@RequestMapping("/group/delete")
	@ResponseBody
	public String delete(Long id){
		groupService.delete(id);
		return CommonUtils.msgCallback(true,"删除成功",null,null);
	}
	
	@RequestMapping("/group/showMember")
	@ResponseBody
	public String showMember(Long id){
		List<GroupMember> members = groupService.showMember(id);
		return CommonUtils.msgCallback(true,"查询成功",null,JSON.toJSONString(members));
	}
	
	@RequestMapping("/group/findGroupById")
	@ResponseBody
	public String findGroupById(Long id){
		Group group = groupService.findGroupById(id);
		return CommonUtils.msgCallback(true,"查询成功",null,JSON.toJSONString(group));
	}
	
	@RequestMapping("/group/mark")
	public String mark() {
		return "group/mark";
	}
 
	@RequestMapping("/group/getmark")
	@ResponseBody
	public String getMark(Model model,Long offSet,Long pageSize,String name,String haveSubmit,HttpServletRequest request){
		User user = (User) request.getSession().getAttribute(contantUtil.SESSION_USER);
		Pager<MemberInfo> memberInfos=groupService.findMemberInfoLimit(offSet, pageSize, name,haveSubmit,user.getId());
		return JSON.toJSONString(memberInfos);
	}
	
	@RequestMapping("/group/addScore")
	@ResponseBody
	public String addScore(Long id,Double score){
		groupService.addScore(id,score);
		return CommonUtils.msgCallback(true,"保存成功",null,null);
	}
	
	@RequestMapping("/group/findStuPro")
	@ResponseBody
	public String getMark( HttpServletRequest request,Long gid,Long sid){
		List<Progress> progress=progressService.findByUId(sid,gid);
		List<GroupMemberAcce> progressAcces=groupService.findMemberAcce(sid,gid);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("pro", progress);
		map.put("file", progressAcces);
		return CommonUtils.msgCallback(true,"查询成功",null,JSON.toJSONString(map));
	}
	
}
